package org.example.runningforum.server;

import org.example.runningforum.mapper.IUserMapper;
import org.example.runningforum.modle.User;
import org.example.runningforum.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private IUserMapper iUserMapper;


    public Integer selectUserCount(){
        return iUserMapper.selectUserCount();
    }


    public User selectUserByUsername(String username){
        return iUserMapper.selectUserByUsername(username);
    }

    public User selectUserById(Integer userId){
        return iUserMapper.selectUserById(userId);
    }

    /*
     * 注册
     * */
    public String enroll(User userInfo) {
        User userInfo1 = new User();
        userInfo1.setUsername(userInfo.getUsername());
        String password = SecurityUtil.encrypt(userInfo.getPassword());
        userInfo1.setPassword(password);
        userInfo1.setMobile(userInfo.getMobile());

        Integer n = iUserMapper.insertUser(userInfo1);
        if(n==1){
            return "注册成功";
        }else {
            return "注册失败";
        }

    }
//进行加密存储
    public Integer updateUser(User user){
        User user1 = new User();
        user1.setUsername(user.getUsername());
        user1.setPassword(SecurityUtil.encrypt(user.getPassword()));
        user1.setMobile(user.getMobile());
        user1.setUserId(user.getUserId());
        return iUserMapper.updateUser(user1);
    }


}
